package thinkinjava4.ch10.controller;

import java.util.ArrayList;
import java.util.List;

public class Controller {
    private List<Event> eventList = new ArrayList<>();
    public static final long TIME_OFFSET = 1000 * 1000 * 1000;
    private static final long START_TIME = System.nanoTime();

    public void addEvent(Event c) {
        eventList.add(c);
    }

    public void run() {
        while (eventList.size() > 0) {
            for (Event e : new ArrayList<>(eventList)) {
                if (e.ready()) {
                    System.out.print("当前时间<" + (System.nanoTime() - START_TIME) / TIME_OFFSET +
                            "s>,事件队列大小<" + eventList.size() + ">,当前响应事件:");
                    System.out.println(e);
                    e.action();
                    eventList.remove(e);
                }
            }
        }
    }
}
